package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.entity.Modify;
import cn.stylefeng.guns.modular.system.entity.Ticket;
import cn.stylefeng.guns.modular.system.model.ModifyDto;
import cn.stylefeng.guns.modular.system.model.TicketDto;
import cn.stylefeng.guns.modular.system.service.ModifyService;
import cn.stylefeng.guns.modular.system.service.TicketService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 罚单记录控制器
 *
 * @author LST
 * @Date 2019
 */
@Controller
@RequestMapping("/ticket")
public class TicketController extends BaseController {

    private static String PREFIX = "/system/car/";

    @Autowired
    private TicketService ticketService;

    @RequestMapping("/ticket_add")
    public String addView() {
        return PREFIX + "ticket_add.html";
    }

    /**
     * 跳转到查看罚单记录
     */
    @Permission
    @RequestMapping("/car_ticket")
    public String userEdit(@RequestParam Long ticketId) {
        if (ToolUtil.isEmpty(ticketId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //获取信息
        Ticket ticket = this.ticketService.getById(ticketId);
        //
        LogObjectHolder.me().set(ticket);
        return PREFIX + "car_ticket.html";
    }

    /**
     * 添加罚单记录
     *
     * @author fengshuonan
     */
    @RequestMapping("/add")
    @Permission
    @ResponseBody
    public Long add(@Valid TicketDto ticketDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Ticket ticket = new Ticket();
        ticket.setTicketNote(ticketDto.getTicketNote());
        ticket.setTicketMoney(ticketDto.getTicketMoney());
        ticket.setTicketDate(ticketDto.getTicketDate());
        ticketService.insertAndGetId(ticket);
        return ticket.getTicketId();
    }

    /**
     * 获取罚单详情
     *
     * @author fengshuonan
     */
    @RequestMapping("/getTicketInfo")
    @ResponseBody
    public Object getTicketInfo(@RequestParam Long ticketId) {
        if (ToolUtil.isEmpty(ticketId)) {
            throw new RequestEmptyException();
        }
        Ticket ticket = ticketService.getById(ticketId);
        return ResponseData.success(ticket);
    }
/*
    *//**
     * 查询车辆列表
     *
     * @author fengshuonan
     *//*
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String carType,
                       @RequestParam(required = false) String carColor) {
            List<Map<String, Object>> cars = carService.selectCars(carType, carColor);
            return new CarWarpper(cars).wrap();
    }


    *//**
     * 添加车辆
     *
     * @author fengshuonan
     *//*
    @RequestMapping("/add")
    *//*@BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class)*//*
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public ResponseData add(@Valid Car car, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        car.setIsDel(GeneralTypeEnum.IS_DELETE.getCode());
        this.carService.addCar(car);
        return SUCCESS_TIP;
    }

    *//**
     * 更新车辆信息
     *
     * @author fengshuonan
     *//*
    @RequestMapping("/edit")
    *//*@BussinessLog(value = "修改管理员", key = "account", dict = UserDict.class)*//*
    @ResponseBody
    public ResponseData edit(@Valid Car car, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.carService.editCar(car);
        return SUCCESS_TIP;
    }

    *//**
     * 删除车辆（逻辑删除）
     *
     * @author fengshuonan
     *//*
    @RequestMapping("/delete")
    *//*@BussinessLog(value = "删除管理员", key = "userId", dict = UserDict.class)*//*
    @Permission
    @ResponseBody
    public ResponseData delete(@RequestParam Long carId) {
        if (ToolUtil.isEmpty(carId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.carService.deleteCar(carId);
        return SUCCESS_TIP;
    }

    *//**
     * 获取车辆详情
     *
     * @author fengshuonan
     *//*
    @RequestMapping("/getCarInfo")
    @ResponseBody
    public Object getUserInfo(@RequestParam Long carId) {
        if (ToolUtil.isEmpty(carId)) {
            throw new RequestEmptyException();
        }
        Car car = carService.getById(carId);
        return ResponseData.success(car);
    }


    @RequestMapping("/addCar")
    @ResponseBody
    public Boolean addCar(){
        Car car = new Car();
        car.setCarColor("111");
        car.setCarInspection(1);
        car.setCarMileage(new Long(121231212));
        car.setRentTimes(1);
        car.setCarColor("111");
        boolean b = carService.saveOrUpdate(car);
        System.out.println(b + "----------------------");
        return b;
    }*/
}
